package com.softeem.score.manager.config;


import com.softeem.score.manager.interceptor.LoginAuthInterceptor;
import com.softeem.score.manager.properties.UserAuthProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Component
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Autowired
    private UserAuthProperties userAuthProperties;        // 注入实体类对象

    @Autowired
    private LoginAuthInterceptor loginAuthInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginAuthInterceptor)
//                .excludePathPatterns("/admin/system/index/login", "/admin/system/index/generateValidateCode")
                .excludePathPatterns(userAuthProperties.getNoAuthUrls())//放行登录和验证码接口
//                .excludePathPatterns("/doc.html", "/v3/api-docs/**", "/doc.html#/home") // 放行 Knife4j 的 UI 路径
                .addPathPatterns("/**");//其它资源全部拦截
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")          // 添加路径规则
                .allowCredentials(true)               // 是否允许在跨域的情况下传递Cookie
                .allowedOriginPatterns("*")           // 允许请求来源的域规则
                .allowedMethods("*")                  // 允许请求的方法
                .allowedHeaders("*");                // 允许所有的请求头
    }
}